<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="市名" prop="areaName">
                    <el-input class="w-[280px]" v-model="queryParams.areaName" />
                </el-form-item>
                <el-form-item label="" prop="parentId">
                    <el-select v-model="queryParams.parentId" class="w-[280px]" placeholder="请选择省份">
                        <el-option v-for="(item, index) in optionsData.province" :key="index" :label="item.areaName" :value="item.areaId"/>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="市名" prop="areaName" min-width="100" />
                <el-table-column label="拼音缩写" prop="areaWord" min-width="100" />
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="city">
import {allProvince, cityDelete, cityLists} from '@/api/city'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {useDictOptions} from "@/hooks/useDictOptions";
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    areaName: '',
    parentId: '',
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: cityLists,
    params: queryParams
})

const { optionsData } = useDictOptions<{
  province: any[]
}>({
  province: {
    api: allProvince
  }
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (areaId: number) => {
    await feedback.confirm('确定要删除？')
    await cityDelete({ areaId })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
